﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject_SEM3_SecurityServices.ObjectClass;
using eProject_SEM3_SecurityServices.Bussiness;

namespace eProject_SEM3_SecurityServices
{
    public partial class frmUpdateEmployee : System.Web.UI.Page
    {
        BDepartment depart;
        BEmployee bemployee;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            depart = new BDepartment();
            bemployee = new BEmployee();
            if(!IsPostBack){
            bindData();
            }
        }

        public void bindData()
        {
            OEmployee oemployee = new OEmployee();
            oemployee = bemployee.GetEmployeeDetails(Convert.ToInt32(Request.QueryString["Id"]));

            tbAddress.Text = oemployee.EmployeeAddress;
            tbBirthDay.Text = oemployee.EmployeeBirthday;
            tbFullName.Text = oemployee.EmployeeFullName;
            tbPass.Text = oemployee.Password;
            tbPhone.Text = oemployee.EmployeePhone.ToString();
            tbRegion.Text = oemployee.Region;
            tbRole.Text = oemployee.Role.ToString();
            tbSalary.Text = oemployee.EmployeeSalary;
            tbUserName.Text = oemployee.UserName;
            tbPass.Text = oemployee.Password;

            List<ODepartment> departList = new List<ODepartment>();
            departList = depart.getDepartmentList();
            ddDepartment.DataSource = departList;
            ddDepartment.DataTextField = "DepartmentName";
            ddDepartment.DataValueField = "DepartmentID";       
            ddDepartment.DataBind();
            ddDepartment.SelectedItem.Text = depart.GetDepartmentDetails(Convert.ToInt32(oemployee.DepartmentId)).DepartmentName;
            
        }

        protected void Button2_Click1(object sender, EventArgs e)
        {
            Response.Redirect("frmUpdateEmployee.aspx?Id=" + Request.QueryString["Id"].ToString());
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                OEmployee oemployee = new OEmployee();
                oemployee.EmployeeID = Convert.ToInt32(Request.QueryString["Id"]);
                oemployee.Password = tbPass.Text;
                oemployee.EmployeeAddress = tbAddress.Text;
                oemployee.EmployeeBirthday = tbBirthDay.Text;
                oemployee.EmployeeFullName = tbFullName.Text;
                oemployee.EmployeeGender = Convert.ToBoolean(ddGender.SelectedValue.ToString());
                oemployee.EmployeePhone = Convert.ToInt32(tbPhone.Text);
                oemployee.EmployeeSalary = tbSalary.Text;
                oemployee.UserName = tbUserName.Text;
                oemployee.Role = Convert.ToInt32(tbRole.Text);
                oemployee.Region = tbRegion.Text;
                oemployee.DepartmentId = Convert.ToInt32(ddDepartment.SelectedValue.ToString());

                bemployee.UpdateEmployee(oemployee);
                Response.Write("<script>alert('updating is successful');</script>");
            }catch(Exception ev){
                Response.Write("<script>alert('updating is not successful');</script>");
            }
        }
    }
}